//
//  CinemaViewController.m
//  豆瓣电影
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaModel.h"
#import "CinemaTableViewCell.h"

@interface CinemaViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrayOfCinema;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation CinemaViewController

-(void)dealloc
{
    [_arrayOfCinema release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self config];
    
    [self handleData];
    
    [self createTableView];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.61 blue:0.68 alpha:1.0];
}

-(void)config
{
    self.arrayOfCinema = [NSMutableArray array];
    
    self.navigationItem.title = @"影院";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)handleData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"cinemalist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSDictionary *dic = [dictionary objectForKey:@"result"];
    
    NSArray *array = [dic objectForKey:@"data"];
    
//    NSLog(@"%ld", array.count);
    
    for (NSDictionary *dic in array) {
        
        CinemaModel *cinemaModel = [[CinemaModel alloc]init];
        
        [cinemaModel setValuesForKeysWithDictionary:dic];
        
        [self.arrayOfCinema addObject:cinemaModel];
    }
//    NSLog(@"%@", self.arrayOfCinema);
    
}


-(void)createTableView
{
    self.tableView = [[UITableView alloc]init];
    
    self.tableView.frame = CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 110);
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CinemaTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOfCinema.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CinemaTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.cinemaModel = [self.arrayOfCinema objectAtIndex:indexPath.row];
    
    return cell;
}



@end























